package com.hfut.eeg.common.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.ArrayList;

@Component
public class ReadUtil {
    @Value("${ReadUtil.address}")
    private String address;

    private int line;

    private int FS = 125;

    /**
     * 获取文件的总行数
     */
    @PostConstruct
    public void getLine(){
        try{
            File file =new File(address);
            if(file.exists()){
                FileReader fr = new FileReader(file);
                LineNumberReader lnr = new LineNumberReader(fr);
                int linenumber = 0;
                while (lnr.readLine() != null)
                    linenumber++;
                lnr.close();
                line = linenumber;
            }else{
                System.out.println("File does not exists!");
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }


    public  ArrayList<ArrayList>getData(int num) throws Exception {
        if(num*FS>line)
            return null;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(address)));
        int i=0;
        bufferedReader.readLine();
        while(i<(num-1)*FS){
            i++;
            bufferedReader.readLine();
        }
        ArrayList<ArrayList>arr = new ArrayList<>();
        for(int j=0;j<16;j++)
            arr.add(new ArrayList());
        for(i=0;i<FS;i++){
            String[] s = bufferedReader.readLine().split("\t");
            for(int j = 0;j<16;j++){
                for (int index=0;index<4;index++){
                    arr.get(j).add(Float.valueOf(s[j].trim()));
                }
            }
        }
        return arr;
    }

}
